package com.restaurant.web.android.service;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.restaurant.domain.Employee;
import com.restaurant.domain.Employeecredential;
import com.restaurant.domain.Person;
import com.restaurant.service.EmployeecredentialService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Component
@RequestMapping("/loginverifyandroid.do")
public class LoginVerifierController {
	
	@Autowired
	private EmployeecredentialService objEmployeecredentialService;
	
	@RequestMapping(method=RequestMethod.POST)
	public String loginVerifierPOSTRequest(ModelMap map, HttpServletRequest request, HttpServletResponse response){
		
		XStream objXStream = new XStream(new DomDriver());
		objXStream.setMode(XStream.NO_REFERENCES);
		objXStream.alias("loginData", Employeecredential.class, Employeecredential.class);
		objXStream.alias("Person", Person.class, Person.class);
		objXStream.alias("Employee", Employee.class, Employee.class);
		Employeecredential employeecredential = (Employeecredential)objXStream.fromXML(request.getParameter("loginData"));
		
		ArrayList<Employeecredential> listEmployeecredentials = new ArrayList<Employeecredential>();
		try {
			listEmployeecredentials = (ArrayList<Employeecredential>) objEmployeecredentialService.loadByHql("from Employeecredential where pin = " + employeecredential.getPin());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		objXStream.alias("Employeecredential", Employeecredential.class);
		
		try{
			if(listEmployeecredentials.size() == 1){
				response.getWriter().print(objXStream.toXML(listEmployeecredentials.get(0)));
			}else{
				response.getWriter().print(objXStream.toXML(null));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

}
